import { defineStore } from "pinia";
import { loginApi } from "../../api/login"
// @ts-ignore
import { ElMessage } from "element-plus"
export const userStore = defineStore("user", {
    persist: true,
    state() {
        let userData: {
            id: 0,
            phone: "",
            username: ""
        } | null = null
        return {
            isLoading: false,
            userData
        }
    },
    actions: {
        async login( username: string, password: string ){
            // this.isLoading = true
            try {
                let {data} = await loginApi( username, password )
                this.userData = data
                ElMessage({
                    type: "success",
                    message: "登录成功"
                })
                this.isLoading = false
            } catch (error: any) {
                this.isLoading = false
                throw new Error( error.message )
            }
            

        }
    }
});
